<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">储位编码</label>
        <el-input
          v-model="query.storageCode"
          clearable
          placeholder="储位编码"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">库区编码</label>
        <el-select v-model="query.zoneCode" class="filter-item" clearable filterable placeholder="请选择">
          <el-option v-for="item in dict.warehouse" :key="item.id" :label="item.label" :value="item.label" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <template slot="right">
          <el-upload
            class="filter-item"
            :action="uploadUrl"
            :on-success="handleSuccess"
            :on-error="handleError"
            :headers="headers"
            :show-file-list="false"
            :file-list="fileList"
          >
            <el-button size="mini" icon="el-icon-upload" type="primary">上传</el-button>
          </el-upload>
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-refresh"
            class="filter-item"
            @click="uploading"
          >同步</el-button>
        </template>
      </crudOperation>

      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="35%"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="auto">
          <el-form-item label="储位编码" prop="storageCode">
            <el-input v-model="form.storageCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="库区编码" prop="zoneCode">
            <el-select v-model="form.zoneCode" clearable filterable placeholder="请选择">
              <el-option v-for="item in dict.warehouse" :key="item.id" :label="item.label" :value="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="巷道号" prop="aisle">
            <el-input v-model="form.aisle" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="层号" prop="layer">
            <el-input v-model="form.layer" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="排号" prop="row">
            <el-input v-model="form.row" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="列号" prop="column">
            <el-input v-model="form.column" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="储位id" />
        <el-table-column prop="storageCode" label="储位编码" />
        <el-table-column prop="palletId" label="载盘号" />
        <el-table-column prop="zoneCode" label="库区编码" />
        <el-table-column prop="aisle" label="巷道号" />
        <el-table-column prop="layer" label="层号" />
        <el-table-column prop="row" label="排号" />
        <el-table-column prop="column" label="列号" />
        <el-table-column prop="locationInventoryStatus" label="货位状态">
          <template slot-scope="scope">
            {{ dict.label.storage_status[scope.row.locationInventoryStatus] }}
          </template>
        </el-table-column>
        <el-table-column label="是否启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="changeStorageStatus(scope.row.id, scope.row.status)"
            >{{
              dict.label.disable[scope.row.status] }}</el-switch>
          </template>
        </el-table-column>

        <el-table-column
          v-if="checkPer(['admin', 'storage:edit', 'storage:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { changeStorageStatus } from '@/api/workStation'
import crudStorage from '@/api/storage'
import { getToken } from '@/utils/auth'
import { uploading } from '@/api/workStation'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, storageCode: null, palletId: null, logicalAddressType: null, zoneCode: null, aisle: null, layer: null, row: null, column: null, locationInventoryStatus: null, status: null }
export default {
  name: 'Storage',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['warehouse', 'storage_status', 'disable'],
  cruds() {
    return CRUD({ title: '储位管理', url: 'api/storage', idField: 'id', sort: 'id,desc', crudMethod: { ...crudStorage }})
  },
  data() {
    return {

      permission: {
        add: ['admin', 'storage:add'],
        edit: ['admin', 'storage:edit'],
        del: ['admin', 'storage:del']
      },
      rules: {
        storageCode: [
          { required: true, message: '储位编码不能为空', trigger: 'blur' }
        ],
        zoneCode: [
          { required: true, message: '库区编码不能为空', trigger: 'blur' }
        ],
        aisle: [
          { required: true, message: '巷道号不能为空', trigger: 'blur' }
        ],
        layer: [
          { required: true, message: '层号不能为空', trigger: 'blur' }
        ],
        row: [
          { required: true, message: '排号不能为空', trigger: 'blur' }
        ],
        column: [
          { required: true, message: '列号不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'storageCode', display_name: '储位编码' },
        { key: 'zoneCode', display_name: '库区编码' }
      ],
      fileList: [],
      uploadUrl: process.env.VUE_APP_BASE_API + '/api/storage/inputStorage',
      headers: {
        'Authorization': getToken() // headers属性中添加token，这个属性是el-upload自带的用来设置上传请求头部
      }
    }
  },
  methods: {
    changeStorageStatus(id, status) {
      changeStorageStatus(id, status).then(data => {
        console.log(data)
      }
      )
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    uploading() {
      uploading().then(data => {
        if (data.meta.code === 200) {
          this.$message.success(data.meta.msg)
        } else {
          this.$message.error(data.meta.msg)
        }
      }).catch((err) => {
        this.$message.error(err)
      })
    },
    handleSuccess(res) {
      if (res.meta.code === 200) {
        this.$message.success('上传成功')
      }
    },
    handleError(res) {
      console.log(res)
      this.$message.error(res)
    }
  }
}
</script>

<style scoped></style>
